﻿package Code {	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class MainApp extends MovieClip{
		
		var myBar:Bar;
		var myBall:Ball;
		var springBase:Base;
		const BALL_RADIUS:Number = 10;
		private var G:Number   = 5;                  // gravitational acceleration constant
		
		public function MainApp() {
			init();
		}
		
		public function init(){
			myBar = new Bar(new Vertex(80,218), new Vertex(400, 220));
			addChild(myBar);
			springBase = new Base(new Vertex(myBar.b.x, 325), myBar.b);
			addChild(springBase);
			myBall = new Ball(new Vertex(myBar.b.x-50, 100), BALL_RADIUS); 
			addChild(myBall);

			start_btn.addEventListener(MouseEvent.CLICK, begin);
			restart_btn.addEventListener(MouseEvent.CLICK, restart);
			
		}
		
		public function begin(e:MouseEvent)
		{
			start_btn.removeEventListener(MouseEvent.CLICK, begin);
			addEventListener(Event.ENTER_FRAME, update);
			pause_btn.addEventListener(MouseEvent.CLICK, onPause);
		}
		
		public function onPause(e:MouseEvent)
		{
			pause_btn.removeEventListener(MouseEvent.CLICK, onPause);
			removeEventListener(Event.ENTER_FRAME, update);
			pause_btn.addEventListener(MouseEvent.CLICK, unPause);
		}
		
		public function unPause(e:MouseEvent)
		{
			pause_btn.removeEventListener(MouseEvent.CLICK, unPause);
			addEventListener(Event.ENTER_FRAME, update);
			pause_btn.addEventListener(MouseEvent.CLICK, onPause);
		}
		
		public function restart(e:MouseEvent)
		{
			graphics.clear();
			removeChild(myBar);
			removeChild(springBase);
			removeChild(myBall);
			init();
		}
		
		public function update(e:Event)
		{
			springBase.springTo(myBar.b);
			moveBall();			updateObjects();
			
			drawAll();
			
			//myBar.y=myBar.b.y;
			
			/*tf = getTimer();
			angle = (tf-ti)/ANIMATION_RATE; //in radians --> parametric equasion like cos(t)
			
			//distance of vertex a to origin:
			var tmp1:Vertex = Vertex.sub(origin, line.a); //dx, dy
			
			//"move" vertex b to above origin( meaning a is at the origin):
			var tmp2:Vertex = Vertex.sub(tmp1, line.b);
			
			//rotate b to b':
			var tmp3:Vertex = rotate(tmp2, angle);
			
			//take rotated point and translate it back (b' back where it should b:
			var tmp4:Vertex = Vertex.add(tmp1, tmp3); //tmp1 has dx, dy and tmp3 is rotated vector
			
			//get ready for new angle-redraw line:
			line.update(line.a, tmp4);
			
			ti = tf;*/
		}
		
		private function moveBall():void {

			// update ball velocity:
			myBall.vy += G;
			myBall.vx += 0; 
			trace(myBall.x+", "+myBall.y+", "+myBall.center.x+", "+myBall.center.y);
			
			// update ball position:
			 myBall.center.y += myBall.vy;	
			 myBall.center.x += myBall.vx;
			
			// check for collisions:
			//checkWalls();
			myBall.checkLines(myBar);
			myBall.checkWalls();
		}
		public function updateObjects()
		{
			myBar.update();
		}
		
		public function drawAll()
		{
			myBall.init();
			myBar.init();
			graphics.clear();
			graphics.lineStyle(5, 0x00BBAA);
			graphics.moveTo(springBase.center.x, springBase.center.y);
			graphics.lineTo(myBar.b.x, myBar.b.y);
		}
	}
	
}
